package com.exam.system.service.impl;

import com.exam.system.dao.ExamQuestionDao;
import com.exam.system.dao.ExamQuestionOptionDao;
import com.exam.system.entity.ExamQuestion;
import com.exam.system.entity.QuestionAndOptionDTO;
import com.exam.system.entity.QuestionOption;
import com.exam.system.service.ExamQuestionOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ExamQuestionOptionServiceImpl implements ExamQuestionOptionService {
    @Autowired
    private ExamQuestionOptionDao examQuestionOptionDao;
    @Autowired
    private ExamQuestionDao examQuestionDao;
    //根据题目id查题目下的所有选项
    @Override
    public List<QuestionOption> getOptionsByQuesId(Integer id) {
        return examQuestionOptionDao.getOptionsByQuesId(id);
    }

    //对题目进行修改并批量修改其题目选项
    @Override
    @Transactional
    public int updateQuestionAndOptions(QuestionAndOptionDTO dto) {
        Integer quesId=dto.getOptions().get(0).getQuesId();
        List<String> tags = dto.getTags();
        String newString=null;
        if(tags.size()!=0){
            StringBuffer string =new StringBuffer();
            for (String tag : tags) {
                string.append(tag).append(",");
            }
             newString =string.substring(0,string.length()-1);
        }
        Integer rightAnswerId = dto.getRightAnswerId();
        String question=dto.getQuestion();
        String analysis = dto.getAnalysis();
        List<Integer> rightAnswerIds = dto.getRightAnswerIds();
        List<QuestionOption> options=dto.getOptions();
        examQuestionDao.updateQuestion(quesId,newString,question,analysis);
        examQuestionOptionDao.delOptionsByQuesId(quesId);
        if(rightAnswerId!=null){
            rightAnswerIds.add(rightAnswerId);
        }
            for (QuestionOption option : options) {
                if(rightAnswerIds.contains(option.getId())){
                    option.setRightAnswer("y");
                }else{
                    option.setRightAnswer("n");
                }
                examQuestionOptionDao.addOptions(option);
                System.out.println(option);
            }
        return 1;
    }

    @Override
    @Transactional
    public int addQuestionAndOptions(QuestionAndOptionDTO dto) {
        List<String> tags = dto.getTags();
        String newString=null;
        if(tags.size()!=0){
            System.out.println("aaaa");
            StringBuffer string =new StringBuffer();
            for (String tag : tags) {
                string.append(tag).append(",");
            }
            newString =string.substring(0,string.length()-1);
        }
        String question=dto.getQuestion();
        String analysis = dto.getAnalysis();
        String type=dto.getType();
        ExamQuestion examQuestion=new ExamQuestion();
        examQuestion.setQuestion(question);
        examQuestion.setType(type);
        examQuestion.setAnalysis(analysis);
        examQuestion.setTags(newString);
        List<Integer> rightAnswerIds = dto.getRightAnswerIds();
        Integer rightAnswerId = dto.getRightAnswerId();
        List<QuestionOption> options=dto.getOptions();
        examQuestionDao.addQuestion(examQuestion);
        if(rightAnswerId!=null){
            rightAnswerIds.add(rightAnswerId);
        }
        for (QuestionOption option : options) {
            option.setQuesId(examQuestion.getId());
            if(rightAnswerIds.contains(option.getId())){
                option.setRightAnswer("y");
            }else{
                option.setRightAnswer("n");
            }
            examQuestionOptionDao.addOptions(option);
            System.out.println(option);
        }
        System.out.println(examQuestion.getId());
        return 1;
    }
}
